package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.${capitalize};
import cn.wolfcode.luowowo.query.${capitalize}Query;
import cn.wolfcode.luowowo.service.I${capitalize}Service;
import cn.wolfcode.luowowo.service.I${capitalize}Service;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/${uncapitalize}")
public class ${capitalize}Controller {
    @Autowired
    private I${capitalize}Service ${uncapitalize}Service;
    //获取所有数据并共享给页面
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ${capitalize}Query qo){
        Page<${capitalize}> page = ${uncapitalize}Service.query(qo);
        model.addAttribute("page",page);
        return "/${uncapitalize}/list";
    }
    //保存操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(${capitalize} ${uncapitalize}){
        if(StringUtils.hasLength(${uncapitalize}.getId())){
            ${uncapitalize}Service.update(${uncapitalize});
        }else {
            ${uncapitalize}.setId(null);
            ${uncapitalize}Service.save(${uncapitalize});
        }
        return JsonResult.success();
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        ${uncapitalize}Service.delete(id);
        return JsonResult.success();
    }
}
